<?php

session_start();
if (!isset($_SESSION['user']))
    header('Location: login.php');

$user = $_SESSION['user'];
$con = mysql_connect("localhost", "root", "");
mysql_select_db("outbysbd", $con);

    $query = "select * from usuario where user_id = '$user'";
    $res = mysql_query($query, $con);
    $user = mysql_fetch_array($res);

if (isset($_POST['titulo'])) {
    $t = $_POST['titulo'];
    $u = $user['user_id'];
    $cat = $_GET['cat'];
    $query = "select * from categoria where nome = '$cat'";
    $res = mysql_query($query, $con);
    $cat = mysql_fetch_array($res);
    
    $c = $cat['cat_id'];
    
   /* $m = $_POST['mensagem'];
    $query = "insert into topico values ('','$t',NOW(), '$u', '$c')";
    mysql_query($query, $con);
    
    $tid = mysql_insert_id();
    $query = "select * from topico where titulo_id = '$tid'";
    $res = mysql_query($query, $con);
    //$top = mysql_fetch_array($res);
    
    $query = "insert into mensagem values ('','$m',NOW(), '$u', '$tid')";
    mysql_query($query, $con);
    
    echo "<meta HTTP-EQUIV='refresh' CONTENT='0;URL=topico.php?t=".$tid."'>"; //redireciona a p�gina*/
    
           /*$mysqli = new mysqli('localhost', 'root', "",'outbysbd');
    $mysqli->autocommit(FALSE);
    $mysqli->query("insert into topico values ('','$t',NOW(), '$u', '$c')");
    $tid = mysql_insert_id();
    $mysqli->query("select * from topico where titulo_id = '$tid'");
    $top = mysql_fetch_array($res_transaction2);    
    $mysqli->query("insert into mensagem values ('','$m',NOW(), '$u', '$tid')");
    
    if($mysqli_error())
        $mysqli->rollback ();
    else
        $mysqli->commit();*/
    $m = $_POST['mensagem'];
    
    mysql_query("SET AUTOCOMMIT=0",$con);
    
    mysql_query("START  TRANSACTION;", $con);
    
        $query = "insert into topico values ('','$t',NOW(), '$u', '$c')";
        $res_transaction1 = mysql_query($query, $con);

        $tid = mysql_insert_id();
        echo $tid;
        $query = "select * from topico where titulo_id = '$tid'";
        $res_transaction2  = mysql_query($query, $con);
        //$top = mysql_fetch_array($res_transaction2);

        $query = "insert into mensagem values ('','$m',NOW(), '$u', '$tid')";
        $res_transaction3 =  mysql_query($query, $con);

        

    if (!($res_transaction1 or $res_transaction2 or $res_transaction3))
     {
        mysql_query("ROLLBACK;", $con);
     }
     else 
     {
        mysql_query("COMMIT;", $con);
     }
        
     
    echo "<meta HTTP-EQUIV='refresh' CONTENT='0;URL=topico.php?t=".$tid."'>"; //redireciona a p�gina*/  
}
        /*$mysqli = new mysqli('localhost', 'root', "",'outbysbd');
    $mysqli->autocommit(FALSE);
    $mysqli->query("insert into topico values ('','$t',NOW(), '$u', '$c')");
    $tid = mysql_insert_id();
    $mysqli->query("select * from topico where titulo_id = '$tid'");
    $top = mysql_fetch_array($res_transaction2);    
    $mysqli->query("insert into mensagem values ('','$m',NOW(), '$u', '$tid')");
    
    if($mysqli_error())
        $mysqli->rollback ();
    else
        $mysqli->commit();*/
    
    /*mysql_query("SET AUTOCOMMIT=0",$con);
    
    mysql_query("START  TRANSACTION;", $con);
    //$dbh ->beginTransaction();
    
        $query = "insert into topico values ('','$t',NOW(), '$u', '$c')";
        $res_transaction1 = mysql_query($query, $con);

        $tid = mysql_insert_id();
        $query = "select * from topico where titulo_id = '$tid'";
        $res_transaction2  = mysql_query($query, $con);
        $top = mysql_fetch_array($res_transaction2);

        $query = "insert into mensagem values ('','$m',NOW(), '$u', '$tid')";
        $res_transaction3 =  mysql_query($query, $con);
 
     if ($res_transaction1 and $res_transaction2 and $res_transaction3)
     {
         mysql_query("COMMIT;", $con);
     }
     else 
     {
         mysql_query("ROLLBACK;", $con);
     }

    */
    
    

?>
